"""自定义 标签 模板中显示所有项目"""

from django import template
from web import models
from django.urls import reverse

register = template.Library()


# 标题浏览显示 所有项目类型 自定义标签函数
@register.inclusion_tag('inclusion/all_project_list.html')
def all_project_list(request):
    # 获取我所创建的所有项目
    my_project_list = models.Project.objects.filter(creator=request.tracer.user)
    # 获取我所参与的项目
    join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user)

    # 返回 变量，传给 上面装饰其中的html中使用
    return {'my': my_project_list, 'join': join_project_list, 'request': request}


# 定制 manage 菜单栏显示
@register.inclusion_tag('inclusion/manage_menu_list.html')
def manage_menu_list(request):
    # 定制 数据列表 存放菜单名 和访问菜单的url
    data_list = [
        {'title': '概览', 'url': reverse('dashboard', kwargs={'project_id': request.tracer.project.id})},
        {'title': '问题', 'url': reverse('issues', kwargs={'project_id': request.tracer.project.id})},
        {'title': '统计', 'url': reverse('statistics', kwargs={'project_id': request.tracer.project.id})},
        {'title': 'wiki', 'url': reverse('wiki', kwargs={'project_id': request.tracer.project.id})},
        {'title': '文件', 'url': reverse('file', kwargs={'project_id': request.tracer.project.id})},
        {'title': '设置', 'url': reverse('setting', kwargs={'project_id': request.tracer.project.id})}
    ]
    # 当前访问的url 与上述字典中的url比较，如果一致(或者以上述url开头)则设置 选中样式
    for item in data_list:
        if request.path_info.startswith(item['url']):
            item['class'] = 'active'
    return {'data_list':data_list}
